﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TransactionFilterTests.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the TransactionFilterTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Tests.Filters
{
    using System.Data.Objects;
    using System.Linq;
    using Data.Filters;
    using Entities;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Tests.Filters.TransactionFilterTests"/> type.
    /// </summary>
    [TestClass]
    public class TransactionFilterTests
    {
        /// <summary>
        /// Mock database
        /// </summary>
        private readonly IObjectSet<Transaction> _mockObjectSet = new MockObjectSet<Transaction>();

        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionFilterTests"/> class.
        /// </summary>
        public TransactionFilterTests()
        {
            _mockObjectSet.AddObject(new Transaction { Id = 1, Title = "Transaction 1", Details = "Transaction Details 1", Account = 1, Amount = 1 });
            _mockObjectSet.AddObject(new Transaction { Id = 2, Title = "Transaction 2", Details = "Transaction Details 2", Account = 2, Amount = 2 });
            _mockObjectSet.AddObject(new Transaction { Id = 3, Title = "Transaction 3", Details = "Transaction Details 3", Account = 3, Amount = 3 });
        }

        /// <summary>
        /// Filter_s the should return everything if the object to comare is null.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnEverythingIfTheObjectToComareIsNull()
        {
            var result = _mockObjectSet.Filter(null).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
        }

        /// <summary>
        /// Filters the should return everything if the object to comare is uninitialized.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnEverythingIfTheObjectToComareIsUninitialized()
        {
            var result = _mockObjectSet.Filter(new Transaction()).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
        }

        /// <summary>
        /// Filters the should return object filtered by id.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredById()
        {
            var result = _mockObjectSet.Filter(new Transaction { Id = 2 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Transaction Details 2", result[0].Details);
        }

        /// <summary>
        /// Filters the should return object filtered by title.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByTitle()
        {
            var result = _mockObjectSet.Filter(new Transaction { Title = "Transaction 1" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(1, result[0].Id);
            Assert.AreEqual("Transaction 1", result[0].Title);
        }

        /// <summary>
        /// Filters the should return object filtered by title with different case.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByTitleWithDifferentCase()
        {
            var result = _mockObjectSet.Filter(new Transaction { Title = "Transaction 2" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Transaction 2", result[0].Title);
        }

        /// <summary>
        /// Filters the should return object filtered by details.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByDetails()
        {
            var result = _mockObjectSet.Filter(new Transaction { Details = "Transaction Details 1" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(1, result[0].Id);
            Assert.AreEqual("Transaction Details 1", result[0].Details);
        }

        /// <summary>
        /// Filters the should return object filtered by details with different case.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByDetailsWithDifferentCase()
        {
            var result = _mockObjectSet.Filter(new Transaction { Details = "TRANSACTION DETAILS 2" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Transaction Details 2", result[0].Details);
        }

        /// <summary>
        /// Filters the type of the should return object filtered by currency.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByAccount()
        {
            var result = _mockObjectSet.Filter(new Transaction { Account = 2 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Transaction 2", result[0].Title);
            Assert.AreEqual(2, result[0].Account);
        }

        /// <summary>
        /// Filters the should return object filtered by amount.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByAmount()
        {
            var result = _mockObjectSet.Filter(new Transaction { Amount = 2 }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(2, result[0].Id);
            Assert.AreEqual("Transaction 2", result[0].Title);
            Assert.AreEqual(2, result[0].Amount);
        }

        /// <summary>
        /// Filters the should return object filtered by combination of fields.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnObjectFilteredByCombinationOfFields()
        {
            var result = _mockObjectSet.Filter(new Transaction { Account = 3, Amount = 3 }).ToList();
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(3, result[0].Id);
            Assert.AreEqual("Transaction 3", result[0].Title);
            Assert.AreEqual(3, result[0].Amount);
        }

        /// <summary>
        /// Filters the should return nothing if mismatching object filtered by combination of fields.
        /// </summary>
        [TestMethod]
        public void TransactionFilterShouldReturnNothingIfMismatchingObjectFilteredByCombinationOfFields()
        {
            var result = _mockObjectSet.Filter(new Transaction { Account = 2, Title = "Something not found" }).ToList();

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
        }
    }
}